from .extensions import db
from pypinyin import  lazy_pinyin

class City(db.Model):
    __tablename__ = 'city'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), unique=True, nullable=False)
    city_code = db.Column(db.String(3), unique=True, nullable=False)
    province_name = db.Column(db.String(20))
    province_id = db.Column(db.Integer, name="supplier_province_id")

    def to_dict(self):
        return {
            'id': str(self.id),
            'name': self.name,
            'three_code': self.city_code,
            'ext_name': self.name,
            'pinyin': ''.join(lazy_pinyin(self.name))
        }


class Province:
    def __init__(self, id0, name, pinyin):
        self.id = str(id0)  # 唯一标识符（如 ID）
        self.name = name
        self.pinyin = pinyin

    # 必须实现 __hash__ 和 __eq__ 才能存入 set
    def __hash__(self):
        return hash(self.id)  # 基于唯一属性（如 id）生成哈希值

    def __eq__(self, other):
        return self.id == other.id

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'ext_name': self.name,
            'pinyin': self.pinyin,
            'city_code': '',
            'children': []
        }


class HotelInfo(db.Model):
    __bind_key__ = 'hotel_db'
    __tablename__ = 'hotel_info'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), name="hotel_name", unique=True, nullable=False)
    hotel_code = db.Column(db.String(16), name="supplier_hotel_code")
    city_name = db.Column(db.String(16), name="city_name")
    city_id = db.Column(db.Integer, name="std_city_id")
    telephone = db.Column(db.String(48), name="tel")
    status = db.Column(db.Integer)
    intro = db.Column(db.String(512), name="short_intro")
    official_rating = db.Column(db.Boolean)
    star_rating = db.Column(db.Integer)
    r_star = db.Column(db.Integer)
    room_count = db.Column(db.Integer)
    open_year = db.Column(db.DateTime)
    renovation_year = db.Column(db.DateTime)
    last_update = db.Column(db.DateTime)

    def to_dict(self):
        return {
            'id': self.id,
            'name': self.name,
            'hotel_code': self.hotel_code,
            'city_name': self.city_name,
            'city_id': self.city_id,
            'telephone': self.telephone,
            'intro': self.intro,
            'official_rating': self.official_rating,
            'status': self.status,
            'star_rating': self.star_rating,
            'r_star': self.r_star,
            'room_count': self.room_count,
            'open_year': self.open_year.strftime('%Y-%m-%d') if self.open_year else None,
            'renovation_year': self.renovation_year.strftime('%Y-%m-%d') if self.renovation_year else None,
            'last_update': self.last_update.strftime('%Y-%m-%d %H:%M:%S') if self.last_update else None,
        }



class HotelRoomTypeInfo(db.Model):
    __bind_key__ = 'hotel_db'
    __tablename__ = 'room_type_info'
    id = db.Column(db.Integer, primary_key=True)
    room_type_code = db.Column(db.String(64), name="room_type_code", unique=True, nullable=False)
    room_type_name = db.Column(db.String(64), name="room_type_name", unique=True, nullable=False)
    hotel_code = db.Column(db.String(16), name="hotel_code")
    hotel_id = db.Column(db.Integer, name="hotel_id")
    floor_range = db.Column(db.String(32), name="floor_range")
    area_range = db.Column(db.String(32), name="area_range")
    has_window = db.Column(db.Integer, name="has_window")
    capacity = db.Column(db.Integer)
    create_time = db.Column(db.DateTime)

    def to_dict(self):
        return {
            'id': self.id,
            'room_type_code': self.room_type_code,
            'room_type_name': self.room_type_name,
            'hotel_code': self.hotel_code,
            'hotel_id': self.hotel_id,
            'floor_range': self.floor_range,
            'area_range': self.area_range,
            'has_window': self.has_window,
            'capacity': self.capacity,
            'create_time': self.create_time.strftime('%Y-%m-%d %H:%M:%S') if self.create_time else None,
        }

